package com.company.huisuo.biz.fragment

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.company.huisuo.R
import com.company.huisuo.base.BaseFragment
import com.company.huisuo.biz.adapter.KRoomAuraAdapter
import com.company.huisuo.biz.bean.MerchantAtmosphereBean
import com.company.huisuo.databinding.FragmentKroomAuraBinding
import com.company.huisuo.repository.MerchantAtmosphereRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 我的K房-气氛组
 */
class KRoomAuraFragment : BaseFragment<FragmentKroomAuraBinding>() {

    private val mAdapter by lazy {
        KRoomAuraAdapter()
    }
    
    private val atmosphereRepository by lazy {
        MerchantAtmosphereRepository()
    }

    companion object {
        fun newInstance(): KRoomAuraFragment {
            return KRoomAuraFragment()
        }
    }

    override fun initView() {
        mBinding?.rcvList?.apply {
            layoutManager = GridLayoutManager(activity, 2, RecyclerView.HORIZONTAL, false)
            adapter = mAdapter
        }
        mAdapter.addOnItemChildClickListener(R.id.tv_button) { adapter, view, position ->
            //点击查看
        }
    }

    override fun initData() {
        // 使用协程获取氛围数据
        CoroutineScope(Dispatchers.Main).launch {
            try {
                // 这里使用一个默认的sessionId，实际项目中应该从其他地方获取
                val sessionId = "default_session_id"
                
                // 在IO线程执行网络请求
                val result = withContext(Dispatchers.IO) {
                    atmosphereRepository.getMerchantAtmosphereList(sessionId)
                }
                
                // 处理结果
                result.onSuccess { atmosphereList ->
                    mAdapter.submitList(atmosphereList)
                }.onFailure { error ->
                    // 加载默认数据作为备用
                    loadDefaultAtmospheres()
                }
            } catch (e: Exception) {
                // 加载默认数据作为备用
                loadDefaultAtmospheres()
            }
        }
    }
    
    /**
     * 加载默认氛围数据作为备用
     */
    private fun loadDefaultAtmospheres() {
        val defaultAtmospheres = listOf(
            MerchantAtmosphereBean(
                id = 1,
                atmosphereCode = "aura_001",
                atmosphereName = "宫廷",
                styleId = 1,
                styleName = "奢华",
                atmosphereWeight = 1,
                atmosphereCover = "https://example.com/aura1.jpg",
                coverMaterialUrl = "",
                selected = false,
                actualType = "default"
            ),
            MerchantAtmosphereBean(
                id = 2,
                atmosphereCode = "aura_002",
                atmosphereName = "浪漫",
                styleId = 2,
                styleName = "温馨",
                atmosphereWeight = 2,
                atmosphereCover = "https://example.com/aura2.jpg",
                coverMaterialUrl = "",
                selected = false,
                actualType = "default"
            ),
            MerchantAtmosphereBean(
                id = 3,
                atmosphereCode = "aura_003",
                atmosphereName = "动感",
                styleId = 3,
                styleName = "活力",
                atmosphereWeight = 3,
                atmosphereCover = "https://example.com/aura3.jpg",
                coverMaterialUrl = "",
                selected = false,
                actualType = "default"
            )
        )
        mAdapter.submitList(defaultAtmospheres)
    }
}